package com.test.auth_db_auto.handler;

import com.test.auth_db_auto.support.BootSecurityProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 登录失败时处理
 * @ Author: .Mr
 * @ ClassName BootLoginFailureHandler
 * @ Description TODO
 * @ date 2021/12/24 11:11
 * @ Version 1.0
 */
@Component
public class BootLoginFailureHandler implements AuthenticationFailureHandler {
    @Autowired
    private BootSecurityProperties properties;

    @Override
    public void onAuthenticationFailure(HttpServletRequest request , HttpServletResponse response, AuthenticationException e) throws IOException, ServletException {
        request.setAttribute("error",e.getMessage());
        request.getRequestDispatcher(properties.getLoginPage()).forward(request,response);
    }
}
